import {
  getStudentSources,
  saveStudentDetailInfo,
  getInitStudentDetail,
  getMyLessonInfo,
  getMyErrorQuestions,
  getStudentLeaves,
  getSubjectPingce,
  addNewScore,
  getStudentRealityScores,
  delStudentRealScore,
  getStudentLevelInfo,
  saveStudentLevelInfo,
  delScoreImage,
  getTotalClasses,
  getTotalStudents,
  saveStudentSubject,
} from  '../../../service/api';
import { myMessage } from '../../../utils/formatDataSource';
export default {
  namespace: 'studentDetailConfig',

  state: {
    menuData: {
      area: [],
      xiaoqu: [],
    },
    initData: {},
    userInfo: {},
    classes: [],
    errors: [],
    leaves: [],
    subjectPc: [],
    newScore: {},
    initScores: [],
    levelPc: {},
    delImgResult: false,
    homeClass: {
      class: [],
      campus: [],
    },
    studentList: {
      data: [],
      total: 0,
    },
  },

  effects: {
    *getStudentSources({ payload }, { call, put }) {
      const response = yield call(getStudentSources, payload);
      yield put({
        type: 's_getStudentSources',
        payload: response,
      });
    },
    *getInitStudentDetail({ payload }, { call, put }) {
      const response = yield call(getInitStudentDetail, payload);
      yield put({
        type: 's_getInitStudentDetail',
        payload: response,
      });
    },
    *saveUserInfo({ payload }, { call, put }) {
      const response = yield call(saveStudentDetailInfo, payload);
      yield put({
        type: 's_saveUserInfo',
        payload: response,
      });
    },
    *getMyLessonInfo({ payload }, { call, put }) {
      const response = yield call(getMyLessonInfo, payload);
      yield put({
        type: 's_getMyLessonInfo',
        payload: response,
      });
    },
    *getMyErrorQuestions({ payload }, { call, put }) {
      const response = yield call(getMyErrorQuestions, payload);
      yield put({
        type: 's_getMyErrorQuestions',
        payload: response,
      });
    },
    *getStudentLeaves({ payload }, { call, put }) {
      const response = yield call(getStudentLeaves, payload);
      yield put({
        type: 's_getStudentLeaves',
        payload: response,
      });
    },
    *getSubjectPingce({ payload }, { call, put }) {
      const response = yield call(getSubjectPingce, payload);
      yield put({
        type: 's_getSubjectPingce',
        payload: response,
      });
    },
    *addNewScore({ payload }, { call, put }) {
      const response = yield call(addNewScore, payload);
      yield put({
        type: 's_addNewScore',
        payload: response,
      });
    },
    *getStudentRealityScores({ payload }, { call, put }) {
      const response = yield call(getStudentRealityScores, payload);
      yield put({
        type: 's_getStudentRealityScores',
        payload: response,
      });
    },
    *delScore({ payload }, { call, put }) {
      const response = yield call(delStudentRealScore, payload);
      yield put({
        type: 's_delScore',
        payload: response,
      });
    },
    *getStudentLevelInfo({ payload }, { call, put }) {
      const response = yield call(getStudentLevelInfo, payload);
      yield put({
        type: 's_getStudentLevelInfo',
        payload: response,
      });
    },
    *saveLevelInfo({ payload }, { call, put }) {
      const response = yield call(saveStudentLevelInfo, payload);
      yield put({
        type: 's_saveLevelInfo',
        payload: response,
      });
    },
    *deleteImg({ payload }, { call, put }) {
      const response = yield call(delScoreImage, payload);
      yield put({
        type: 's_deleteImg',
        payload: response,
      });
    },
    *totalClasses({ payload }, { call, put }) {
      const response = yield call(getTotalClasses, payload);
      yield put({
        type: 's_totalClasses',
        payload: response,
      });
    },
    *getTotalStudents({ payload }, { call, put }) {
      const response = yield call(getTotalStudents, payload);
      yield put({
        type: 's_getTotalStudents',
        payload: response,
      });
    },
    *saveStudentSubject({ payload }, { call, put }) {
      const response = yield call(saveStudentSubject, payload);
      yield put({
        type: 's_saveStudentSubject',
        payload: response,
      });
    },
  },

  reducers: {
    s_getStudentSources(state, { payload }) {
      if (payload) {
        state.menuData = payload.data;
      }
      return {
        ...state,
      };
    },
    s_getInitStudentDetail(state, { payload }) {
      if (payload) {
        const { data } = payload;
        const objData = JSON.parse(data);
        if (objData.compulsory) {
          objData.compulsory = JSON.parse(objData.compulsory);
        }
        if (objData.likeStyle) {
          objData.likeStyle = JSON.parse(objData.likeStyle);
        }
        if (objData.likeSubject) {
          objData.likeSubject = JSON.parse(objData.likeSubject);
        }
        state.initData = objData;
      }
      return {
        ...state,
      };
    },
    s_saveUserInfo(state, { payload }) {
      const { status } = payload;
      if (status === 0) {
        myMessage.success('提交成功', 2);
      } else {
        myMessage.success('提交失败，' + payload.errorMsg, 2);
      }
      return {
        ...state,
      };
    },
    s_getMyLessonInfo(state, { payload }) {
      if (payload) {
        const { data } = payload;
        state.classes = data;
      }
      return {
        ...state,
      };
    },
    s_getMyErrorQuestions(state, { payload }) {
      if (payload) {
        const { data } = payload;
        state.errors = data;
      }
      return {
        ...state,
      };
    },
    s_getStudentLeaves(state, { payload }) {
      if (payload) {
        const { data } = payload;
        data.map((d, i) => (d.key = i + 1));
        state.leaves = data;
      }
      return {
        ...state,
      };
    },
    s_getSubjectPingce(state, { payload }) {
      if (payload) {
        const { data } = payload;
        data.map((d, i) => (d.key = i + 1));
        state.subjectPc = data;
      }
      return {
        ...state,
      };
    },
    s_addNewScore(state, { payload }) {
      if (payload) {
        const { data } = payload;
        state.newScore = data;
      }
      return {
        ...state,
      };
    },
    s_getStudentRealityScores(state, { payload }) {
      if (payload) {
        const { data } = payload;
        data.map((d, i) => {
          d.key = i + 1;
        });
        state.initScores = data;
      }
      return {
        ...state,
      };
    },
    s_delScore(state, { payload }) {
      if (payload) {
        const { status } = payload;
        if (status === 0) {
          myMessage.success('删除成功', 2);
        } else {
          myMessage.error('删除失败', 2);
        }
      }
      return {
        ...state,
      };
    },
    s_getStudentLevelInfo(state, { payload }) {
      if (payload) {
        const { data } = payload;
        state.levelPc = data;
      }
      return {
        ...state,
      };
    },
    s_saveLevelInfo(state, { payload }) {
      const { status } = payload;
      if (status === 0) {
        myMessage.success('定档成功', 2);
      } else {
        myMessage.error('定档失败', 2);
      }
      return {
        ...state,
      };
    },
    s_deleteImg(state, { payload }) {
      const { status } = payload;
      if (status === 0) {
        // message.success('删除成功', 2);
        state.delImgResult = true;
      } else {
        state.delImgResult = false;
        myMessage.error('删除失败', 2);
      }
      return {
        ...state,
      };
    },
    s_totalClasses(state, { payload }) {
      const { data } = payload;
      state.homeClass = data;
      return {
        ...state,
      };
    },
    s_getTotalStudents(state, { payload }) {
      const { data } = payload;
      data.data.map(d => {
        d.key = d.id;
      });
      state.studentList = data;
      return {
        ...state,
      };
    },
    s_saveStudentSubject(state, { payload }) {
      const { status } = payload;
      if (status === 0) {
        myMessage.success('提交成功', 2);
      } else {
        myMessage.error('提交失败', 2);
      }
      return {
        ...state,
      };
    },
  },
};
